home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / doBlendShape.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  9.9 KB  |  399 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //  MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //  Creation Date:  June, 1997
  22. //  Author:         Karan Singh
  23. //
  24. //  Description:
  25. //      This script assembles one or more commands to perform blendShape
  26. //        creation or editing operations.
  27. //
  28.  
  29. global proc int findBlendShapeConn(string $blendShapeNode,
  30.                                    string $lp1[])
  31.  
  32. //
  33. //    Description:
  34. //        Utility method used by swapBlendShapeTargets to find which
  35. //        plug in the $lp1 string array is a plug on the specified
  36. //        blendShape node.
  37. //
  38. //    Return value: -1 if plug is not found, else index of found plug
  39. //
  40. {
  41.     int $ii;
  42.     int $result = -1;
  43.     string $buff[];
  44.     for ($ii = 0; $ii < size($lp1); $ii++) {
  45.         tokenize($lp1[$ii],".",$buff);
  46.         if (size($buff) && $buff[0] == $blendShapeNode) {
  47.             $result = $ii;
  48.             break;
  49.         }
  50.     }
  51.     return $result;
  52. }
  53.  
  54. proc string targetAttribute(string $target)
  55. //
  56. // Description:
  57. //  Return a string that corresponds to the world-space output attribute
  58. //  for the target.
  59. //
  60. {
  61.     string $targetShape = $target;
  62.     string $isTransform[] = `ls -type transform $target`;
  63.     if (size($isTransform)) {
  64.         string $rels[] = `listRelatives -type controlPoint -ni -pa $target`;
  65.         if (size($rels)) {
  66.             $targetShape = $rels[0];
  67.         }
  68.     }
  69.  
  70.     string $futureAttr;
  71.     string $nt = `nodeType $targetShape`;
  72.     if      ($nt == "mesh")         { $futureAttr = ".w[0]";  }
  73.     else if ($nt == "nurbsSurface")    { $futureAttr = ".ws[0]"; }
  74.     else if ($nt == "subdiv")        { $futureAttr = ".ws[0]"; }    
  75.     else if ($nt == "nurbsCurve")     { $futureAttr = ".ws[0]"; }
  76.     else if ($nt == "lattice")        { $futureAttr = ".wl[0]"; }
  77.     string $result = ($targetShape+$futureAttr);
  78.     return $result;
  79. }
  80.  
  81. global proc    int swapBlendShapeTargets(int $bsp,
  82.                                       string $blendShapeNode,
  83.                                       string $target1,
  84.                                       string $target2)
  85. //
  86. // Method: swapBlendShapeTargets
  87. //
  88. // Arguments:
  89. //        int $bsp (whether or not the blendShape was specified)
  90. //        string $blendShapeNode (the blendShape node, if $bsp == true)
  91. //        string $target1 (1st target to swap)
  92. //        string $target2 (2nd target to swap)
  93. //
  94. // Return: 0 if error occurs, else 1
  95. //
  96. {
  97.     string $node = $blendShapeNode;
  98.  
  99.     string $attr  = targetAttribute($target1);
  100.     string $attr2 = targetAttribute($target2);
  101.  
  102.     string $lp1[]=`listConnections -p true -t blendShape $attr`;
  103.     string $lp2[]=`listConnections -p true -t blendShape $attr2`;
  104.  
  105.     // if one of the targets is not connected, return an error
  106.     //
  107.     if (size($lp1) == 0 || size($lp2) == 0) {
  108.         string $errString;
  109.         if (size($lp1) == 0) {
  110.             $errString = ($target1+" is not a target. ");
  111.         }
  112.         if (size($lp2) == 0) {
  113.             $errString += ($target2+" is not a target. ");
  114.         }
  115.         error($errString);
  116.         return 0;
  117.     }
  118.     
  119.     string $conn1 = $lp1[0];
  120.     string $conn2 = $lp2[0];
  121.  
  122.     // one of the targets is a target for > 1 blendShape
  123.     //
  124.     if (size($lp1) > 1 || size($lp2) > 1)
  125.     {
  126.         if (! $bsp) {
  127.             error("Must specify blendShape node. Shape is a target for more than one blendShape.");
  128.             return 0;
  129.         } else {
  130.             // verify that the specified blendShape is attached to both
  131.             // of these targets
  132.             //
  133.             int $which = findBlendShapeConn($blendShapeNode,$lp1);
  134.             if ($which == -1) {
  135.                 error($target1+" is not a target on "+$blendShapeNode);
  136.                 return 0;
  137.             }
  138.             $conn1 = $lp1[$which];
  139.             
  140.             $which = findBlendShapeConn($blendShapeNode,$lp2);
  141.             if ($which == -1) {
  142.                 error($target2+" is not a target on "+$blendShapeNode);
  143.                 return 0;
  144.             }
  145.             $conn2 = $lp2[$which];
  146.         }
  147.         $node = $blendShapeNode;
  148.     } else {
  149.         // verify that the target shapes are both on the same blendShape
  150.         //
  151.         string $buff1[], $buff2[];
  152.         tokenize($conn1,".",$buff1);
  153.         tokenize($conn2,".",$buff2);
  154.         if (0 == size($buff1) || 0 == size($buff2) || $buff1[0]!=$buff2[0])
  155.         {
  156.             error("Target shapes are on different blendShape nodes");
  157.             return 0;
  158.         }
  159.         $node = $buff1[0];
  160.     }
  161.  
  162.     // form the command to swap the targets
  163.     //
  164.     string $cmd = "disconnectAttr " + $attr +  " "  +  $conn1 +
  165.         ";disconnectAttr " + $attr2 + " "  +  $conn2 +
  166.         ";connectAttr  "   + $attr +  " "  +  $conn2 +
  167.         ";connectAttr  "   + $attr2 + " "  +  $conn1 + ";";
  168.  
  169.     // determine the blendShape indices in order to swap the target
  170.     // attribute alias names
  171.     //
  172.     int $index1 = bsTargetIndex($conn1);
  173.     int $index2 = bsTargetIndex($conn2);
  174.     if ($index1 == -1 || $index2 == -1) {
  175.         warning("Couldn't determine indices for attribute alias swap.");
  176.     } else if ($index1 != $index2) {
  177.         string $alias1 = `aliasAttr -q ($node+".w["+$index1+"]")`;
  178.         string $alias2 = `aliasAttr -q ($node+".w["+$index2+"]")`;
  179.  
  180.         // form the command to swap the attribute aliases
  181.         //
  182.         if (size($alias1) && size($alias2)) {
  183.             $cmd += ("aliasAttr -rm "+($node+"."+$alias1)+";");
  184.             $cmd += ("aliasAttr -rm "+($node+"."+$alias2)+";");
  185.             $cmd += ("aliasAttr "+$alias2+" "+($node+".w["+$index1+"];"));
  186.             $cmd += ("aliasAttr "+$alias1+" "+($node+".w["+$index2+"];"));
  187.         }
  188.     }
  189.     
  190.     if (catch(evalEcho($cmd))) {
  191.         undo;
  192.     }
  193.     return 1;
  194. }
  195.  
  196. global proc  doBlendShape(int $mode,
  197.                           int $tween,
  198.                           int $top,
  199.                           int $bsp,
  200.                           string $bsn,
  201.                           int $bstp,
  202.                           string $bstn,
  203.                           float $bstw,
  204.                           int $deltgt)
  205. //
  206. //  Input Arguments:
  207. //        int $mode (1 == add, 3 == remove, 2 == swap)
  208. //        int $tween (whether or not we are in in-between mode)
  209. //        int $top (whether or not to check topology)
  210. //        int $bsp (whether or not the blendShape was specified)
  211. //        string $bsn (blendShape node, if it was specified)
  212. //        int $bstp (whether or not the blendShapeTarget was specified)
  213. //        int $bstn (target name, if it was specified)
  214. //        float $bstw (target weight, if target was specified)
  215. //        int $deltgt (whether or not to delete the targets)
  216. //
  217. //  Return Value:
  218. //      None.
  219. //
  220. {
  221.     string $sel[]=`ls -sl`;
  222.     int $cnt = size($sel);
  223.  
  224.     if ($cnt == 0)
  225.     {
  226.         // at a minimum the base shape on the blendShape must be selected
  227.         //
  228.         error("Must select the base shape corresponding to the target.");
  229.         return;
  230.     }
  231.  
  232.     if ($cnt == 1) {
  233.         // error: no target is specified and none is selected
  234.         //
  235.         if (!$bstp || size($bstn) == 0) {
  236.             error ("Must select both the target and the base shape.");
  237.             return;
  238.         }
  239.  
  240.         $sel[1] = $sel[0];
  241.         $sel[0] = $bstn;
  242.         $cnt = 2;
  243.     }
  244.  
  245.     // if mode == swap or mode == add  and the number of selected objects
  246.     // is 1 ...
  247.     //
  248.     if ($cnt < 2 && $mode!=3)
  249.     {
  250.         if ($deltgt)
  251.         {
  252.             if (!$bsp || $bsn=="")
  253.             {
  254.                 // if no blendShape was specified, it must be selected
  255.                 //
  256.                 $bsn=$sel[$cnt-1];
  257.             }
  258.             
  259.             if (nodeType($bsn) != "blendShape") {
  260.                 error("Must select a blendShape node");
  261.                 return;
  262.             }
  263.  
  264.             // delete all the targets from the blendShape
  265.             //
  266.             string $tgts[]=`blendShape -q -t $bsn`;
  267.             string $delc = "delete ";
  268.             for ($tg in $tgts)
  269.                 $delc += ($tg + " ");
  270.             evalEcho $delc;
  271.         }
  272.         else
  273.         {
  274.             error ("Specify at least two shapes: target(s) and base.");
  275.         }
  276.         return;
  277.     }
  278.  
  279.     $cmd = "blendShape -e ";
  280.  
  281.     // check topology 
  282.     //
  283.     if (!$top)
  284.     {
  285.         $cmd += (" -tc " + $top);
  286.     }        
  287.  
  288.     if (!$bsp || $bsn=="")
  289.     {
  290.         $bsn=$sel[$cnt-1];
  291.     }
  292.  
  293.     // if blendShape node was not specified let the 
  294.     // last sel item (an aux. object) be used so that
  295.     // the deformer command can try and figure out the 
  296.     // blendShape node from it.
  297.  
  298.  
  299.     // add target mode
  300.     //
  301.     if ($mode==1)
  302.     {
  303.         int $wc=`blendShape -q -wc $bsn`;
  304.         if ($tween) {
  305.           if ($bstp) {
  306.               if ($tween > $wc) {
  307.                 error("Inbetween index must be <= total number of targets.");
  308.                 return;
  309.               }
  310.               $wc = $tween-1;
  311.               $cmd += " -ib";
  312.           } else {
  313.               error("Must specify the target to add inbetweens.");
  314.               return;
  315.           }
  316.  
  317.           // find the multiIndex that corresponds to this logical index
  318.           //
  319.           int $mi = bsMultiIndexForTarget($bsn,$wc);
  320.           if (-1 != $mi) {
  321.               $wc = $mi;
  322.           }
  323.         } else {
  324.             // find the multiIndex of the next available target
  325.             //            
  326.             if ($wc > 0) {
  327.                 int $mi = bsMultiIndexForTarget($bsn,$wc-1);
  328.                 if (-1 != $mi) {
  329.                     $wc = $mi+1;
  330.                 }
  331.             }
  332.         }
  333.         
  334.         for ($i=0;$i<$cnt-1;$i++)
  335.         {
  336.             if (!$tween)
  337.             {
  338.                 $cmd +=(" -t " + $sel[$cnt-1] + " " + ($wc+$i) +" " + 
  339.                     $sel[$i] + " 1"); 
  340.             }
  341.             else
  342.             {
  343.                 float $ib;
  344.                 if ($cnt == 2) {
  345.                   $ib = $bstw;
  346.                 } else {
  347.                   $ib = $bstw*($i+1);
  348.                 }
  349.                 $cmd +=(" -t " + $sel[$cnt-1] + " " + $wc +" " + 
  350.                         $sel[$i] + " "+$ib);
  351.             }
  352.         }
  353.         $cmd +=" "+ $bsn;
  354.     }
  355.     // remove target mode
  356.     //
  357.     else if ($mode==3)
  358.     {
  359.         int $wc=`blendShape -q -wc $bsn`;
  360.         $cmd += " -rm";
  361.  
  362.         for ($i=0;$i<=$cnt-1;$i++)
  363.         {
  364.             $cmd +=(" -t " + $sel[$cnt-1] + " " + $wc + " " + 
  365.                     $sel[$i] + " 1"); 
  366.         }
  367.         $cmd +=" "+ $bsn;
  368.     }
  369.     // swap target mode
  370.     //
  371.     else if ($mode==2)
  372.     {
  373.         if (size($sel)!=2)
  374.         {
  375.             error("Specify exactly two target shapes to swap");
  376.             return;
  377.         }
  378.         else
  379.         {
  380.             swapBlendShapeTargets($bsp,$bsn,$sel[0],$sel[1]);
  381.             return;
  382.         }
  383.     }
  384.     evalEcho($cmd);
  385.  
  386.     // delete the targets if requested
  387.     //
  388.     if ($deltgt) {
  389.         string $tgts[]=`blendShape -q -t $bsn`;
  390.         string $delc = "delete ";
  391.         for ($tg in $tgts)
  392.             $delc += ($tg + " ");
  393.         evalEcho $delc;
  394.     }
  395.     return;
  396. }
  397.  
  398.  
  399.